'''关于Beautiful Soup库的使用
中国大学排名定向爬虫实例,并可视化结果
'''
from bs4 import BeautifulSoup
import requests
import bs4
import re
import matplotlib.pyplot as plt


plt.rcParams['font.sans-serif'] = ['KaiTi']

def getHTMLText(url):
    try:
        r=requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        print("爬取失败")
        return ""

def fiilUnivList(ulist,html):
    soup=BeautifulSoup(html,"html.parser")
    for tr in soup.find('tbody').children:
        if isinstance(tr,bs4.element.Tag):
            tds=tr('td')
            mypattern=re.compile('[\u4e00-\u9fa5]+')
            name = mypattern.findall(tds[2].text)
            ulist.append([tds[0].string.strip(),name[0].strip(),tds[5].text.strip()])


def printUnivList(ulist,num):
    tplt="{0:^10}\t{1:{3}^10}\t{2:^10}"
    print(tplt.format("排名","学校名称","总分",chr(12288)))
    for i in range(num):
        u=ulist[i]
        print(tplt.format(u[0],u[1],u[2],chr(12288)))

def main():

    name_list = []
    num_list = []

    uinfo=[]
    url=r"D:\爬虫学习资料\universityRank.html"
    htmlfile = open(url, 'r',encoding='utf-8')
    #html=getHTMLText(url)
    html = htmlfile.read()
    fiilUnivList(uinfo,html)
    printUnivList(uinfo,20) #20univs
    for item in uinfo:
        name_list.append(item[1])
        num_list.append(float(item[2]))

    '可视化'
    plt.figure(figsize=(20, 4))  # 设置画布大小
    l1=plt.bar(range(len(num_list[:15])), num_list[:15],  tick_label=name_list[:15])
    plt.legend(handles=[l1, ], labels=['总分', ], loc='best')
    plt.show()
    htmlfile.close()

if __name__=='__main__':
    main()
